package com.tz.system.user.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tz.platform.page.PageCondition;
import com.tz.platform.utils.ReturnUtil;
import com.tz.platform.utils.StringUtil;
import com.tz.system.role.model.TMgtRole;
import com.tz.system.role.service.TMgtRoleService;
import com.tz.system.user.UserUtils;
import com.tz.system.user.model.TMgtUser;
import com.tz.system.user.model.TMgtUserRole;
import com.tz.system.user.model.UserInfo;
import com.tz.system.user.service.TMgtUserService;
import com.tz.system.user.service.UserRoleService;

@Controller
@RequestMapping("/system/user/tMgtUser")
public class TMgtUserController {
	
	@Autowired
	private TMgtUserService tMgtUserService;
	
	@Autowired
	private UserRoleService userRoleService;
	
	@Autowired
	private TMgtRoleService roleService;
	
	/**
	 * 用户管理列表信息
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public ModelAndView list(PageCondition pl, Model model, HttpServletRequest request) {
		//设置页面属性
		model.addAllAttributes(pl.getParamsCondition());
		//增加排序条件
		pl.setSortBys("id", "desc");
		pl = tMgtUserService.findByPage(pl);
		// 3、设置查询结果
		model.addAttribute("pl", pl);
		return new ModelAndView("/system/user/t-mgt-user-list");
	}
	
	/**
	 * 增加用户页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toAddPage")
	public ModelAndView toAddPage(Model model,String id) {
		if(!StringUtil.msIsEmpty(id)){
			TMgtUser entity = tMgtUserService.queryById(Integer.parseInt(id));
			model.addAttribute("entity", entity);
		}
		return new ModelAndView("/system/user/t-mgt-user-form");
	}
	
	/**
	 * 增加保存信息
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/addSave")
	@ResponseBody
	public Map<String,Object> addSave(TMgtUser entity) {
		return tMgtUserService.addSave(entity);
	}
	
	
	/**
	 * 更新保存信息
	 * @param model
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/updateSave")
	@ResponseBody
	public Map<String,Object> updateSave(TMgtUser entity) {
		return tMgtUserService.updateSave(entity);
	}
	
	
	/**
	 * 根据id删除信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteObj")
	@ResponseBody
	public Map<String,Object> deleteTMgtUser(String id) {
		return tMgtUserService.removeById(Integer.valueOf(id));
	}
	
	/**
	 * 跳转至修改密码页面
	 * @param model
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/toUpdatePassWord")
	public ModelAndView toResetPassWord(HttpServletRequest request, Model model) {
		UserInfo userinfo = UserUtils.getSessionUser(request);
		model.addAttribute("message", tMgtUserService.toResetPassWord() );
		model.addAttribute("userinfo", userinfo);
		return new ModelAndView("/system/user/t_user_change_passwd");
	}
	
	/**
	 * 修改用户密码
	 * @return
	 */
	@RequestMapping(value = "/updatePassword")
	@ResponseBody
	public Map<String,Object> updatePassword(String userId, String oldPasswd, String newUserPassword) {
		return tMgtUserService.updatePasswd(userId, oldPasswd, newUserPassword);
	}
	
	/**
	 * 重置用户密码
	 * @return
	 */
	@RequestMapping(value = "/resetPassword")
	@ResponseBody
	public Map<String,Object> resetPasswordUser(String id) {
		return tMgtUserService.resetPasswordById(Integer.parseInt(id));
	}
	
	/**
	 * 跳转至个人信息页面
	 * @param model
	 * @param condition
	 * @return
	 */
	@RequestMapping(value = "/toProfile")
	public ModelAndView toProfile(HttpServletRequest request, Model model) {
		UserInfo userinfo = UserUtils.getSessionUser(request);
		TMgtUser entity = tMgtUserService.queryByUserId(userinfo.getUserId());
		model.addAttribute("entity", entity);
		return new ModelAndView("/system/user/t_user_profile");
	}
	
	/**
	 * toSetProvate:(设置用户角色信息). <br/>
	 * @author changhz
	 * @param model
	 * @param id
	 * @param sysCodeTransfor
	 * @return
	 */
	@RequestMapping(value = "/toSetPrivate")
	public ModelAndView toSetProvate(Model model,String id) {
		TMgtUser entity = tMgtUserService.queryById(Integer.parseInt(id));
		List<TMgtUserRole> userRoleList = userRoleService.queryByUserId(entity.getUserId());
		model.addAttribute("userRoleList", userRoleList);
		model.addAttribute("entity", entity);
		List<TMgtRole> roleList = roleService.queryAll();
		model.addAttribute("roleList", roleList);
		return new ModelAndView("/system/user/t_mgt_user_private");
	}
	
	/**
	 * 更新用户的角色信息
	 * @param model
	 * @param prdType
	 * @return
	 */
	@RequestMapping(value = "/updateTMgtUserRole")
	@ResponseBody
	public Map<String,Object> updateTMgtUserRole(Model model,TMgtUser entity,String roleCode) {
		try {
			tMgtUserService.updateSaveRole(entity,roleCode);
		} catch (Exception e) {
			e.printStackTrace();
			return ReturnUtil.returnMessage("fail", "更新失败！");
		}
		return ReturnUtil.returnMessage("success", "更新成功！");
	}
	
}

